﻿Imports Control_Vuelos_BL
Imports Control_Vuelos_BE

Public Class frmTabla_MLugar
    Inherits frmBaseMantenimiento
    Private _M_Lugar As New M_LugarBL
    Private _M_LugarBE As New M_LugarBE
    Private opcion As Integer = 0

    Private Sub frmTabla_MLugar_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed
        SG_Control_Vuelos_Main.RibbonPageGroup1.Visible = True
        SG_Control_Vuelos_Main.RibbonPageGroup2.Visible = True
        SG_Control_Vuelos_Main.RibbonPageGroup3.Visible = True
        SG_Control_Vuelos_Main.RibbonPageGroup4.Visible = True
        SG_Control_Vuelos_Main.RibbonPageGroup5.Visible = True
        SG_Control_Vuelos_Main.RibbonPageGroup6.Visible = True
        SG_Control_Vuelos_Main.RibbonPageGroup7.Visible = True
        SG_Control_Vuelos_Main.RibbonPageGroup9.Visible = True
    End Sub

    Private Sub frmTabla_MLugar_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        AddHandler BarBtnAgregar.ItemClick, AddressOf pr_RegistroNuevo
        AddHandler BarBtnModificar.ItemClick, AddressOf pr_Modificar
        AddHandler BarBtnEliminar.ItemClick, AddressOf pr_Eliminar
        AddHandler BarBtnGrabar.ItemClick, AddressOf pr_Grabar
        AddHandler BarBtnCancelar.ItemClick, AddressOf pr_Cancelar
        AddHandler BarBtnRefresh.ItemClick, AddressOf pr_Refresh
        AddHandler BarBtnImprimir.ItemClick, AddressOf pr_Imprimir
        pr_Refresh()
        InicioBtn()
    End Sub

    Private Sub pr_Imprimir()
        Dim RptLugar As New XRptM_Lugar()
        RptLugar.DataSource = GridControl1.DataSource
        RptLugar.DataMember = "M_Lugar"

        RptLugar.ShowRibbonPreviewDialog()

    End Sub

    Private Sub pr_Refresh()
        GridControl1.DataSource = _M_Lugar.ListarM_Lugar("")
    End Sub

    Private Sub InicioBtn()
        BarBtnAgregar.Enabled = True
        BarBtnModificar.Enabled = True
        BarBtnEliminar.Enabled = True
        BarBtnGrabar.Enabled = False
        BarBtnCancelar.Enabled = False
        BarBtnRefresh.Enabled = True
    End Sub

    Private Sub ProcessBtn()
        BarBtnAgregar.Enabled = False
        BarBtnModificar.Enabled = False
        BarBtnEliminar.Enabled = False
        BarBtnGrabar.Enabled = True
        BarBtnCancelar.Enabled = True
        BarBtnRefresh.Enabled = False
    End Sub

    Private Sub pr_Eliminar()
        If GridView1.RowCount > 0 Then
            Dim row As DataRow = TryCast(GridView1.GetDataRow(GridView1.GetSelectedRows()(0)), DataRow)
            If Not row Is Nothing Then
                If MsgBox("¿Está seguro de eliminar el registro?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Desición") = MsgBoxResult.Yes Then
                    _M_LugarBE.CodLugar = ModM_Lugar1.gCodLugar.EditValue
                    _M_LugarBE.NomLugar = ModM_Lugar1.gNomLugar.EditValue
                    If _M_Lugar.pr_EliminarM_Lugar(_M_LugarBE) Then
                        pr_Refresh()
                        FocusedFila()
                    End If
                End If
            End If
        End If
        opcion = 0
    End Sub

    Private Sub pr_Modificar()
        If GridView1.RowCount > 0 Then
            Dim row As DataRow = TryCast(GridView1.GetDataRow(GridView1.GetSelectedRows()(0)), DataRow)
            If Not row Is Nothing Then
                XtraTabPage1.Text = " General (EDITAR) "
                ModM_Lugar1.gCodLugar.EditValue = row("CodLugar")
                ModM_Lugar1.gCodLugar.Properties.ReadOnly = True
                ModM_Lugar1.gNomLugar.EditValue = row("NomLugar")
                ModM_Lugar1.gNomLugar.Properties.ReadOnly = False
                ModM_Lugar1.gNomLugar.Focus()
                GridControl1.Enabled = False
                opcion = 2
            End If
        End If
        ProcessBtn()
    End Sub

    Private Sub pr_Cancelar()
        GridControl1.Enabled = True
        GridControl1.Focus()
        FocusedFila()
        opcion = 0
        XtraTabPage1.Text = " General "
        InicioBtn()
    End Sub

    Private Sub pr_Grabar()
        If ModM_Lugar1.DxValidationProvider1.Validate Then
            XtraTabPage1.Text = " General "
            GridControl1.Enabled = True
            GridControl1.Focus()
            _M_LugarBE.CodLugar = ModM_Lugar1.gCodLugar.EditValue
            _M_LugarBE.NomLugar = ModM_Lugar1.gNomLugar.EditValue
            If opcion = 1 Then
                If _M_Lugar.pr_AgregarM_Lugar(_M_LugarBE) Then
                    pr_Refresh()
                End If
            End If
            If opcion = 2 Then
                If _M_Lugar.pr_ModificarM_Lugar(_M_LugarBE) Then
                    pr_Refresh()
                End If
            End If
        End If
        GridControl1.Enabled = True
        GridControl1.Focus()
        GridControl1.Select()
        opcion = 0
        InicioBtn()
    End Sub

    Private Sub pr_RegistroNuevo()
        XtraTabPage1.Text = " General (NUEVO) "
        ModM_Lugar1.gCodLugar.EditValue = ""
        ModM_Lugar1.gCodLugar.Properties.ReadOnly = False
        ModM_Lugar1.gNomLugar.EditValue = ""
        ModM_Lugar1.gNomLugar.Properties.ReadOnly = False
        ModM_Lugar1.gCodLugar.Focus()
        GridControl1.Enabled = False
        opcion = 1
        ProcessBtn()
    End Sub

    Private Sub GridView1_FocusedRowChanged(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs) Handles GridView1.FocusedRowChanged
        If GridView1.RowCount > 0 Then
            FocusedFila()
        End If
    End Sub

    Private Sub FocusedFila()
        If GridView1.RowCount > 0 Then
            Dim row As DataRow = TryCast(GridView1.GetDataRow(GridView1.GetSelectedRows()(0)), DataRow)
            If Not row Is Nothing Then
                ModM_Lugar1.gCodLugar.EditValue = row("CodLugar")
                ModM_Lugar1.gCodLugar.Properties.ReadOnly = True
                ModM_Lugar1.gNomLugar.EditValue = row("NomLugar")
                ModM_Lugar1.gNomLugar.Properties.ReadOnly = True
            End If
        End If
    End Sub

End Class